package com.ruoyi.web.controller.car;

import com.ruoyi.car.domain.CatRepair;
import com.ruoyi.car.service.ICatRepairService;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.config.Global;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.file.FileUploadUtils;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.framework.util.ShiroUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.List;

/**
 * 【请填写功能名称】Controller
 * 
 * @author ruoyi
 * @date 2020-06-24
 */
@Controller
@RequestMapping("/ruoyi/repair")
public class CatRepairController extends BaseController
{
    private String prefix = "ruoyi/repair";

    @Autowired
    private ICatRepairService catRepairService;

    @RequiresPermissions("ruoyi:repair:view")
    @GetMapping()
    public String repair()
    {
        return prefix + "/repair";
    }

    /**
     * 查询【请填写功能名称】列表
     */
    @RequiresPermissions("ruoyi:repair:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(CatRepair catRepair)
    {
        startPage();
        catRepair.setcPeople(ShiroUtils.getLoginName());
        List<CatRepair> list = catRepairService.selectCatRepairList(catRepair);
        return getDataTable(list);
    }

    /**
     * 查询【请填写功能名称】列表
     */
    @RequiresPermissions("ruoyi:repair:list")
    @PostMapping("/lists")
    @ResponseBody
    public List<CatRepair> lists(CatRepair catRepair)
    {
        catRepair.setcPeople(ShiroUtils.getLoginName());
        List<CatRepair> list = catRepairService.selectCatRepairList(catRepair);
        return list;
    }

    /**
     * 导出【请填写功能名称】列表
     */
    @RequiresPermissions("ruoyi:repair:export")
    @Log(title = "【请填写功能名称】", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(CatRepair catRepair)
    {
        List<CatRepair> list = catRepairService.selectCatRepairList(catRepair);
        ExcelUtil<CatRepair> util = new ExcelUtil<CatRepair>(CatRepair.class);
        return util.exportExcel(list, "repair");
    }

    /**
     * 新增【请填写功能名称】
     */
    @GetMapping("/add")
    public String add()
    {
        return prefix + "/add";
    }

    /**
     * 新增保存【请填写功能名称】
     */
    @RequiresPermissions("ruoyi:repair:add")
    @Log(title = "【请填写功能名称】", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(@RequestParam("file") MultipartFile file, CatRepair catRepair)throws IOException
    {
        //上传文件路径
        String filePach = Global.getUploadPath();
        //上传并返回新文件名称
        String fileName = FileUploadUtils.upload(filePach, file);
        catRepair.setcData(fileName);
        catRepair.setcStatus(0L);
        catRepair.setcDepa(ShiroUtils.getdept());
        catRepair.setcPicture(ShiroUtils.getavatar());
        catRepair.setcPeople(ShiroUtils.getLoginName());
        return toAjax(catRepairService.insertCatRepair(catRepair));
    }

    /**
     * 提交申请
     */
    @Log(title = "报销业务", businessType = BusinessType.UPDATE)
    @PostMapping( "/submitApply")
    @ResponseBody
    public AjaxResult submitApply(Long id,CatRepair catRepair) {

        catRepair.setcId(id);
        catRepair.setcStatus(1L);
        int succ=catRepairService.updateCatRepair(catRepair);
        if(succ==1){
            return success("提交成功");
        }else{
            return error("提交失败");
        }
    }


    /**
     * 修改【请填写功能名称】
     */
    @GetMapping("/leadedit/{cId}")
    public String edit(@PathVariable("cId") Long cId, ModelMap mmap)
    {
        CatRepair catRepair = catRepairService.selectCatRepairById(cId);
        mmap.put("catRepair", catRepair);
        return prefix + "/leadedit";
    }

    /**
     * 修改保存【请填写功能名称】
     */
    @RequiresPermissions("ruoyi:repair:edit")
    @Log(title = "【请填写功能名称】", businessType = BusinessType.UPDATE)
    @PostMapping("/leadedit")
    @ResponseBody
    public AjaxResult editSave(CatRepair catRepair)
    {
        return toAjax(catRepairService.updateCatRepair(catRepair));
    }

    /**
     * 删除【请填写功能名称】
     */
    @RequiresPermissions("ruoyi:repair:remove")
    @Log(title = "【请填写功能名称】", businessType = BusinessType.DELETE)
    @PostMapping( "/remove")
    @ResponseBody
    public AjaxResult remove(String ids)
    {
        return toAjax(catRepairService.deleteCatRepairByIds(ids));
    }

    @RequiresPermissions("ruoyi:repair:removes")
    @Log(title = "【请填写功能名称】", businessType = BusinessType.DELETE)
    @PostMapping( "/removes")
    @ResponseBody
    public AjaxResult removes(Long id)
    {
        int succ= catRepairService.deleteCatRepairById(id);
        if(succ==1){
            return success("撤销成功");
        }else{
            return error("撤销失败");
        }

    }
    /**
     * 查询车辆维修管理列表
     */
    @RequiresPermissions("ruoyi:repair:lists")
    @PostMapping("/lists/{cId}")
    @ResponseBody
    public TableDataInfo lists(@PathVariable("cId") String cId,CatRepair catRepair)
    {
        catRepair.setcCarld(cId);
        startPage();
        List<CatRepair> lists = catRepairService.selectCat(catRepair);
        return getDataTable(lists);
    }

    /**
     * 导出【请填写功能名称】列表
     */
    @RequiresPermissions("ruoyi:repair:exportrepair")
    @Log(title = "【请填写功能名称】", businessType = BusinessType.EXPORT)
    @PostMapping("/exportrepair")
    @ResponseBody
    public AjaxResult exportCar(CatRepair catRepair)
    {
        List<CatRepair> list = catRepairService.selectRepair(catRepair);
        ExcelUtil<CatRepair> util = new ExcelUtil<CatRepair>(CatRepair.class);
        return util.exportExcel(list, "车辆维修支出");
    }
}
